/*
 * Copyright (c) 2024 Fujian Newland Auto-ID Tech.Co.,Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/**
 * 字符串工具
 */
export class StringUtils {
  /**
   * 判断字符串是否为空
   * @param str
   * @returns
   */
  static isNull(str: string): boolean {
    if (str === undefined || str.length === 0) {
      return true;
    }
    return false
  }

  /**
   * 截取字符串尾部指定长度的字符
   * @param str
   * @param length
   * @returns
   */
  static getTail(str: string, length: number): string {
    if (str === undefined || str.length === 0) {
      return '';
    }
    if (length > str.length) {
      return str;
    }
    const totalLength = str.length;
    const startIndex = totalLength - length;
    return str.slice(startIndex);
  }

  /**
   * 替换字符串中的占位符（单个）
   * @param str 原字符串
   * @param placeholder  需要替换的标签
   * @param value 替换的值
   * @returns 返回替换后的字符串
   */
  static replacePlaceholder(str: string, placeholder: string, value: string) {
    return str.replace(`{${placeholder}}`, value);
  }

  /**
   * 获取23位的随机数字字符串
   * @returns 返回字符串
   */
  static generate23DigitNumberString(): string {
    let num = '';

    for (let i = 0; i < 23; i++) {
      num += Math.floor(Math.random() * 10);
    }

    return num.padStart(23, '0');
  }
}